﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Common {
	public class ConvertHelper {

		/// <summary>把string 转 int32,小于0返回0,否则返回int值</summary>
		/// <param name="str"></param>
		/// <returns>返回>=0的int型</returns>
		public static int Cint0(object str) {
			if (str != null) {
				if (string.IsNullOrEmpty(str.ToString())) {
					return 0;
				}
				return int.Parse(str.ToString());
			}
			return 0;
		}

		#region IQueryable转换到DataTable
		/// <summary>
		/// IQueryable转DataTable
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="enumerable"></param>
		/// <returns></returns>
		public static DataTable ConvertToDataTable<T>(IEnumerable<T> enumerable) {
			var dataTable = new DataTable();
			foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(T))) {
				dataTable.Columns.Add(pd.Name, pd.PropertyType);
			}
			foreach (T item in enumerable) {
				var Row = dataTable.NewRow();

				foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T))) {
					Row[dp.Name] = dp.GetValue(item);
				}
				dataTable.Rows.Add(Row);
			}
			return dataTable;
		}

		//将IEnumerable<T>类型的集合转换为DataTable类型 
		public static DataTable LingToDataTable<T>(IEnumerable<T> varlist) {   //定义要返回的DataTable对象
			DataTable dtReturn = new DataTable();
			// 保存列集合的属性信息数组
			PropertyInfo[] oProps = null;
			if (varlist == null) return dtReturn;//安全性检查
			//循环遍历集合，使用反射获取类型的属性信息
			foreach (T rec in varlist) {
				//使用反射获取T类型的属性信息，返回一个PropertyInfo类型的集合
				if (oProps == null) {
					oProps = ((Type)rec.GetType()).GetProperties();
					//循环PropertyInfo数组
					foreach (PropertyInfo pi in oProps) {
						Type colType = pi.PropertyType;//得到属性的类型
						//如果属性为泛型类型
						if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
						== typeof(Nullable<>))) {   //获取泛型类型的参数
							colType = colType.GetGenericArguments()[0];
						}
						//将类型的属性名称与属性类型作为DataTable的列数据
						dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
					}
				}
				//新建一个用于添加到DataTable中的DataRow对象
				DataRow dr = dtReturn.NewRow();
				//循环遍历属性集合
				foreach (PropertyInfo pi in oProps) {   //为DataRow中的指定列赋值
					dr[pi.Name] = pi.GetValue(rec, null) == null ?
						DBNull.Value : pi.GetValue(rec, null);
				}
				//将具有结果值的DataRow添加到DataTable集合中
				dtReturn.Rows.Add(dr);
			}
			return dtReturn;//返回DataTable对象
		}
		#endregion

		#region 实现对IList到DataTable的转换

		//// <summary>
		/// 实现对IList到DataSet的转换
		/// </summary>
		/// <param name="ResList">待转换的IList</param>
		/// <returns>转换后的DataTable</returns>
		public static DataTable IListToDataTable<T>(IList<T> ResList) {
			//定义DataTable
			DataTable TempDT = new DataTable();
			//此处遍历IList的结构并建立同样的DataTable
			System.Reflection.PropertyInfo[] p = ResList[0].GetType().GetProperties();
			foreach (System.Reflection.PropertyInfo pi in p) {
				TempDT.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
			}
			for (int i = 0; i < ResList.Count; i++) {
				IList TempList = new ArrayList();
				//将IList中的一条记录写入ArrayList
				foreach (System.Reflection.PropertyInfo pi in p) {
					object oo = pi.GetValue(ResList[i], null);
					TempList.Add(oo);
				}

				object[] itm = new object[p.Length];
				//遍历ArrayList向object[]里放数据
				for (int j = 0; j < TempList.Count; j++) {
					itm.SetValue(TempList[j], j);
				}
				//将object[]的内容放入DataTable
				TempDT.LoadDataRow(itm, true);
			}

			//返回DateTable
			return TempDT;
		}
		#endregion
	}
}
